package com.panda.mall.coupon.couponInfo.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.panda.mall.common.mallCoupon.dto.CouponInfoDto;
import com.panda.mall.common.mallCoupon.entity.MallCouponInfo;
import com.panda.mall.coupon.couponInfo.service.MallCouponInfoService;
import com.panda.mall.coupon.couponInfo.mapper.MallCouponInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
* @author tucy123
* @description 针对表【mall_coupon_info(优惠券信息表)】的数据库操作Service实现
* @createDate 2023-07-19 17:11:51
*/
@Service
public class MallCouponInfoServiceImpl extends ServiceImpl<MallCouponInfoMapper, MallCouponInfo> implements MallCouponInfoService{

    @Autowired
    MallCouponInfoMapper mallCouponInfoMapper;

    /**
     * 条件分页查询
     * @param couponInfoDto
     * @return
     */
    @Override
    public Page<MallCouponInfo> pageList(CouponInfoDto couponInfoDto) {
        Page<MallCouponInfo> page = new Page<>(couponInfoDto.getPageNum(),couponInfoDto.getPageSize());

        return mallCouponInfoMapper.pageList(page,couponInfoDto);
    }

    /**
     * 新增或修改
     * @param mallCouponInfo
     * @return
     */
    @Override
    public Boolean saveOrUpdateCoupon(MallCouponInfo mallCouponInfo) {
        if (ObjectUtil.isEmpty(mallCouponInfo.getCouponCode())){
            mallCouponInfo.setCouponCode(createCouponCode());
        }
        return this.saveOrUpdate(mallCouponInfo);
    }

    /**
     * 优惠码
     * @return
     */
    public String createCouponCode() {
        UUID uuid = UUID.randomUUID();
        String code = String.valueOf(uuid).replace("-", "");

        List<MallCouponInfo> couponInfos = this.lambdaQuery().eq(MallCouponInfo::getCouponCode, code).list();
        if (ObjectUtil.isNotEmpty(couponInfos)){
           code = createCouponCode();
        }
        return code;
    }

    /**
     * 优惠券详情
     * @param id
     * @return
     */
    @Override
    public MallCouponInfo details(Long id) {
        return this.getById(id);
    }

    /**
     * 删除优惠券
     * @param ids
     * @return
     */
    @Override
    public Boolean delete(List<Long> ids) {
        int i = mallCouponInfoMapper.deleteBatchIds(ids);
        return i != 0;
    }

}




